package com.renwei.yunlong.http;

import android.content.Context;
import android.util.Log;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.renwei.yunlong.R;
import com.renwei.yunlong.utils.LogUtil;

import org.xutils.common.Callback;
import org.xutils.common.util.KeyValue;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import github.opensource.dialog.BeToastUtil;


/**
 * 描述：XUtils网络框架
 */

public class HttpPresenter {
    private static final String TAG = "HttpPresenter";
    private RequestParams params;
    private int requstId;
    private HttpTaskListener listener;
    private Context context;
    private Callback.Cancelable cancelable;
    private KProgressHUD mProgressDialog;//进度窗体

    private boolean isShowDialog = true;
    private String content = "加载中";

    public HttpPresenter(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    public void get() {
        if (!CheckNetwork.isNetworkConnected(context)) {
            BeToastUtil.get().showCenterInfoMsg(context, "无网络连接,请检查网络");
            return;
        }
        if (params == null) {
            System.out.println("params is null");
            return;
        }

        cancelable = x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onCancelled(CancelledException arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (listener != null) {
                    BeToastUtil.get().showTopWrongMsg(context, context.getResources().getString(R.string.net_work_error));
                    listener.onException(requstId, arg0.toString());
                }
            }

            @Override
            public void onFinished() {
                // TODO Auto-generated method stub
                hideInfoProgressDialog();
            }

            @Override
            public void onSuccess(String arg0) {
                // TODO Auto-generated method stub
                if (listener != null) {
                    listener.onSuccess(requstId, arg0);
                }
                Log.e("返回数据", arg0);
            }

        });

        if (isShowDialog) {
            showInfoProgressDialog(context, content);
        }

    }


    public void post() {
        if (!CheckNetwork.isNetworkConnected(context)) {
            BeToastUtil.get().showTopWrongMsg(context, "无网络连接,请检查网络");
            return;
        }
        if (params == null) {
            System.out.println("params is null");
            return;
        }

        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onCancelled(CancelledException arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (listener != null) {
                    listener.onException(requstId, arg0.getMessage().toString());
                }
                LogUtil.w(TAG, "--------------------------------------start--------------------------------------------------------------------------------------------------------------------------------------------------------\r\n");
                LogUtil.w(TAG, "接口：" + params.getUri() + "\r\n");
                for (KeyValue keyValue : params.getStringParams()) {
                    LogUtil.w(TAG, "参数---:\r\n [key]:" + keyValue.key + "\r\n[value]:" + keyValue.getValueStr());
                }
                LogUtil.w(TAG, "返回参数：" + arg0 + "\r\n");
                LogUtil.w(TAG, "--------------------------------------end--------------------------------------------------------------------------------------------------------------------------------------------------------");
            }

            @Override
            public void onFinished() {
                // TODO Auto-generated method stub
                hideInfoProgressDialog();

            }

            @Override
            public void onSuccess(String arg0) {
                LogUtil.w(TAG, "--------------------------------------start--------------------------------------------------------------------------------------------------------------------------------------------------------\r\n");
                LogUtil.w(TAG, "接口：" + params.getUri() + "\r\n");
                LogUtil.w(TAG, "异常：" + arg0 + "\r\n");
                for (KeyValue keyValue : params.getStringParams()) {
                    LogUtil.w(TAG, "参数---:\r\n [key]:" + keyValue.key + "\r\n[value]:" + keyValue.getValueStr());
                }
                LogUtil.w(TAG, "返回参数：" + arg0 + "\r\n");
                LogUtil.w(TAG, "--------------------------------------end--------------------------------------------------------------------------------------------------------------------------------------------------------");
                // TODO Auto-generated method stub
                if (listener != null) {
                    listener.onSuccess(requstId, arg0);
                }
            }
        });
        x.Ext.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        if (isShowDialog) {
            showInfoProgressDialog(context, content);
        }

    }


    public RequestParams getParams() {
        return params;
    }

    /**
     *
     */
    public HttpPresenter putParam(String key, String value) {
        if (params != null) {
            params.addBodyParameter(key, value);
        }
        return this;
    }

    /**
     * 传json
     */
    public HttpPresenter putjson(String json) {
        if (params != null) {
            params.setAsJsonContent(true);
            params.setBodyContent(json);
        }
        return this;
    }

    /**
     * 传文件
     */
    public HttpPresenter putFile(String key, File file) {
        if (params != null) {
            params.addBodyParameter(key, file);
        }
        return this;
    }

    public HttpPresenter putHead(String key, String value) {
        if (params != null) {
            params.addHeader(key, value);
        }
        return this;
    }

    public HttpPresenter setRequsetId(int requsetId) {
        this.requstId = requsetId;
        return this;
    }

    public HttpPresenter setUrl(String url) {
        params = new RequestParams(url);
        params.setConnectTimeout(1000 * 10);
        params.setReadTimeout(1000 * 10);
        return this;
    }

    public HttpPresenter setUrl(String url, int timeout) {
        params = new RequestParams(url);
        params.setConnectTimeout(timeout);
        params.setReadTimeout(timeout);
        return this;
    }

    public HttpPresenter setCallBack(HttpTaskListener listener) {
        if (listener != null) {
            this.listener = listener;
        }
        return this;
    }

    //用来设置请求是是否显示progressdialog
    public void setShowDialog(boolean isShowDialog) {
        this.isShowDialog = isShowDialog;
    }

    public void setshowDialogcontent(String msg) {
        this.content = msg;
    }

    /**
     * 显示进度框
     *
     * @param str
     */
    public void showInfoProgressDialog(Context context, final String... str) {
        if (context == null) {
            return;
        }
        if (mProgressDialog == null) {
            mProgressDialog = new KProgressHUD(context);
            mProgressDialog.setCancellable(false);
        }
        if (str.length == 0) {
            mProgressDialog.setLabel("加载中...");
        } else {
            mProgressDialog.setLabel(str[0]);
        }

        if (!mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    /**
     * 隐藏等待条
     */
    public void hideInfoProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }


}
